// pages/order/waimai.js
const app = getApp();
const nt_util = require('../../utils/nt_util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    queryBean: {},
    goods: [],
    goodsList: [],
    cart: {
      count: 0,
      total: 0,
      price:'',
      list: {}
    },
    showCartDetail: false,
    classifySeleted:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var queryBean = JSON.parse(options.queryBean);
    that.setData({
      queryBean: queryBean
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    nt_util.that = this;
    this.getMenusByAppid();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  getMenusByAppid: function () {
    // 获取获取菜品信息
    nt_util.httpRequest(nt_util.api.getMenusByAppid, '', {
      repastId: this.data.queryBean.repastId,
      date: this.data.queryBean.date
    }, function (result) {
      var res = result.data;
        nt_util.that.setData({
          goodsList: res.goodsList,
          goods: res.goods,
        })
      });
  },
  tapAddCart: function (e) {
    this.addCart(e.target.dataset.id);
  },
  tapReduceCart: function (e) {
    this.reduceCart(e.target.dataset.id);
  },
  addCart: function (id) {
    var num = this.data.cart.list[id] || 0;
    this.data.cart.list[id] = num + 1;
    this.countCart();
  },
  reduceCart: function (id) {
    var num = this.data.cart.list[id] || 0;
    if (num <= 1) {
      delete this.data.cart.list[id];
      this.data.goods[id].num = undefined;
    } else {
      this.data.cart.list[id] = num - 1;
    }
    this.countCart();
  },
  countCart: function () {
    var count = 0,
      total = 0;
    var order_goods = [];
    for (var id in this.data.cart.list) {
      var goods = this.data.goods[id];
      count += this.data.cart.list[id];
      total += goods.price * this.data.cart.list[id];
      goods.num = this.data.cart.list[id];
      goods.total = goods.price * this.data.cart.list[id];
      order_goods.push(goods);

    }
    this.data.cart.order_goods = order_goods;
    this.data.cart.count = count;
    this.data.cart.total = total;
    app.globalData.order.cart = this.data.cart;
    this.setData({
      cart: this.data.cart,
      goods: this.data.goods
    });
  },
  follow: function () {
    this.setData({
      followed: !this.data.followed
    });
  },
  onGoodsScroll: function (e) {
    var scale = e.detail.scrollWidth / 570,
      scrollTop = e.detail.scrollTop,
      h = 0,
      classifySeleted,
      len = this.data.goodsList.length;
    this.data.goodsList.forEach(function (classify, i) {
      var he = i * 34;
      var _h = classify.goods.length * (80);
      if (scrollTop >= h + he) {
        classifySeleted = classify.id;
      }
      h += _h;
    });
    this.setData({
      classifySeleted: classifySeleted
    });
  },
  tapClassify: function (e) {
    var id = e.target.dataset.id;
    this.setData({
      classifyViewed: id
    });
    var self = this;
    setTimeout(function () {
      self.setData({
        classifySeleted: id
      });
    }, 100);
  },
  showCartDetail: function () {
    this.setData({
      showCartDetail: !this.data.showCartDetail
    });
  },
  hideCartDetail: function () {
    this.setData({
      showCartDetail: false
    });
  },
  submit: function (e) {
    var url = '/pages/order/waimaiorder?queryBean=' + JSON.stringify(this.data.queryBean);
    wx.navigateTo({
      url: url
    })

  }
})